package com.jacen.fzxs.util;


/**
 * 微信Xml工具类
 * @author Jacen
 * 2014年8月20日 下午7:59:09
 */
public class XmlUtil {
	
	public static String getTextXml(String fromUserName,String toUserName,String context){
		StringBuilder sb = new StringBuilder(getTopXml())
		.append(getToUserNameXml(toUserName))
		.append(getFromUserNameXml(fromUserName))
		.append(getCreateTimeXml())
		.append(getMsgTypeXml("text"))
		.append(getContentXml(context))
		.append(getEndXml());
		return sb.toString();
	}
	
	public static String getImageXml(String fromUserName,String toUserName,String mediaId){
		StringBuilder sb = new StringBuilder(getTopXml())
		.append(getToUserNameXml(toUserName))
		.append(getFromUserNameXml(fromUserName))
		.append(getCreateTimeXml())
		.append(getMsgTypeXml("image"))
		.append(getImageXml(mediaId))
		.append(getEndXml());
		return sb.toString();
	}
	
	public static String getVoiceXml(String fromUserName,String toUserName,String mediaId){
		StringBuilder sb = new StringBuilder(getTopXml())
		.append(getToUserNameXml(toUserName))
		.append(getFromUserNameXml(fromUserName))
		.append(getCreateTimeXml())
		.append(getMsgTypeXml("voice"))
		.append(getVoiceXml(mediaId))
		.append(getEndXml());
		return sb.toString();
	}
	
	public static String getLocationXml(String toUserName, String fromUserName, String mediaId) {
		StringBuilder sb = new StringBuilder(getTopXml())
				.append(getToUserNameXml(toUserName))
				.append(getFromUserNameXml(fromUserName))
				.append(getCreateTimeXml())
				.append(getMsgTypeXml("location"))
				.append(getVoiceXml(mediaId))
				.append(getEndXml());
				return sb.toString();
	}
	
	public static String getMenuXml(String fromUserName, String toUserName,
			String context) {
		StringBuilder sb = new StringBuilder(getTopXml())
		.append(getToUserNameXml(toUserName))
		.append(getFromUserNameXml(fromUserName))
		.append(getCreateTimeXml())
		.append(getMsgTypeXml("text"))
		.append(getContentHtmlXml("<p>? : 获取帮助</p>"
				+ "0 ：查询信息\n"
				+ "1 ：修改信息\n"))
		.append(getEndXml());
		return sb.toString();
	}
	
	private static String getVoiceXml(String mediaId) {
		return "<Voice><MediaId><![CDATA["+mediaId+"]]></MediaId></Voice>";
	}

	private static String getTopXml(){
		return "<xml>";
	}
	
	private static String getEndXml(){
		return "</xml>";
	}
	
	private static String getCreateTimeXml(){
		return "<CreateTime>"+System.currentTimeMillis()+"</CreateTime>";
	}
	
	private static String getMsgTypeXml(String type){
		return "<MsgType><![CDATA["+type+"]]></MsgType>";
	}
	
	private static String getToUserNameXml(String toUserName){
		return "<ToUserName><![CDATA["+toUserName+"]]></ToUserName>";
	}
	
	private static String getFromUserNameXml(String fromUserName){
		return "<FromUserName><![CDATA["+fromUserName+"]]></FromUserName>";
	}
	
	private static String getContentXml(String context){
		return "<Content><![CDATA["+context+"]]></Content>";
	}
	
	private static String getContentHtmlXml(String context){
		return "<Content><![CDATA["+context+"]]></Content>";
	}
	
	private static String getImageXml(String mediaId){
		return "<Image><MediaId><![CDATA["+mediaId+"]]></MediaId></Image>";
	}

	
}
